package com.morningstar.system.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.base.BaseController;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.controller.entity.RoleMenu;
import com.morningstar.system.dao.entity.SysRole;
import com.morningstar.system.dao.entity.SysRoleMenu;
import com.morningstar.system.service.SysRoleMenuService;
import com.morningstar.system.service.SysRoleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

/**
 * 角色信息接口
 *
 * @author yanglianshan
 * @since 2023-06-28
 */
@Slf4j
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    private static final String PERMISSION = "system.role";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;
    private static final String PERMISSION_PERMISSION = PERMISSION + ".permission";

    @Resource
    private SysRoleService roleService;

    @Resource
    private SysRoleMenuService roleMenuService;

    /**
     * 角色前端列表
     *
     * @param commonQuery 查询条件
     * @return 角色列表
     */
    @PostMapping(value = "/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysRole>> list(@RequestBody CommonQuery<SysRole, SysRole> commonQuery) {
        SysRole query = commonQuery.getQuery();
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            queryWrapper.like(StringUtils.isNotBlank(query.getRoleName()), SysRole::getRoleName, query.getRoleName());
            queryWrapper.like(StringUtils.isNotBlank(query.getRoleKey()), SysRole::getRoleKey, query.getRoleKey());
        }
        queryWrapper.orderByAsc(SysRole::getRoleSort).orderByDesc(SysRole::getCreateTime);
        return Result.successData(roleService.page(commonQuery.getPage(), queryWrapper));
    }

    /**
     * 新增角色
     *
     * @param sysRole 角色实体类
     * @return 基本结果类
     */
    @PostMapping
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody SysRole sysRole) {
        ServiceCheckResult serviceCheckResult = roleService.checkAddAndUpdate(sysRole);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        roleService.save(sysRole);
        return Result.successNoData();
    }

    /**
     * 更新角色信息
     *
     * @param sysRole 角色实体类
     * @return 基本结果类
     */
    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody SysRole sysRole) {
        ServiceCheckResult serviceCheckResult = roleService.checkAddAndUpdate(sysRole);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        roleService.updateById(sysRole);
        return Result.successNoData();
    }

    /**
     * 删除角色信息
     *
     * @param roleId 角色Id
     * @return 基本结果类
     */
    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("roleId") Long roleId) {
        //todo 角色有人分配角色，不能删除
        roleService.removeById(roleId);
        return Result.successNoData();
    }

    /**
     * 获取角色详细
     *
     * @param roleId 角色Id
     * @return 角色信息
     */
    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<SysRole> detail(@RequestParam("roleId") Long roleId) {
        return Result.successData(roleService.getById(roleId));
    }

    /**
     * 角色选择列表
     *
     * @return 角色选择列表
     */
    @GetMapping("/select")
    @SaCheckLogin
    public Result<List<SysRole>> select() {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getStatus, "0");
        wrapper.orderByAsc(SysRole::getRoleSort);
        wrapper.orderByDesc(SysRole::getCreateTime);
        List<SysRole> list = roleService.list(wrapper);
        return Result.successData(list);
    }

    /**
     * 获取角色权限信息
     *
     * @param roleId 角色Id
     * @return 角色权限列表
     */
    @GetMapping("/permission")
    @SaCheckPermission(PERMISSION_PERMISSION)
    public Result<List<Long>> permission(@RequestParam("roleId") String roleId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId)
                .eq(SysRoleMenu::getCheckType, "1");
        List<Long> result = roleMenuService.list(wrapper)
                .stream()
                .map(SysRoleMenu::getMenuId)
                .toList();
        return Result.successData(result);
    }

    /**
     * 设置角色权限
     *
     * @param roleMenu 设置角色实体类
     * @return 基本结果类
     */
    @PostMapping("/permission")
    @SaCheckPermission(PERMISSION_PERMISSION)
    public Result<Object> setPermission(@RequestBody RoleMenu roleMenu) {
        roleMenuService.setPermission(roleMenu);
        return Result.successNoData();
    }
}
